import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class GeneralSettings {

	private static String className = "GeneralSettings: ";
	private static String settingsFilePath = "config//general_settings.xml";

	public static int scheduleTickInterval = 1000;
	public static String tdToolFilePath = "/usr/bin/tdtool";
	public static String tellstickDeviceFilePath = "/etc/tellstick.conf";
	public static int tellstickRepetitions = 10;
	public static double latitude = 50.0;
	public static double longitude = 50.0;
	public static String timeZone = "America/Los_Angeles";

	public static void saveSettings() {
		String funtionName = "saveSettings";
		DEBUG(className + funtionName);

		// START CONFIG
		Properties properties = new Properties();
		properties.setProperty("scheduleTickInterval",
				Integer.toString(scheduleTickInterval));
		properties.setProperty("tdToolFilePath", tdToolFilePath);
		properties.setProperty("tellstickDeviceFilePath",
				tellstickDeviceFilePath);
		properties.setProperty("tellstickRepetitions",
				Integer.toString(tellstickRepetitions));
		properties.setProperty("latitude",
				Double.toString(latitude));
		properties.setProperty("longitude",
				Double.toString(longitude));
		properties.setProperty("timeZone", timeZone);
		// END CONFIG

		try {
			FileOutputStream fos = new FileOutputStream(settingsFilePath);
			properties.storeToXML(fos, "GeneralSettings", "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void loadSettings() {
		String funtionName = "loadSettings";
		DEBUG(className + funtionName);
		Properties properties = new Properties();

		try {
			FileInputStream fis = new FileInputStream(settingsFilePath);
			properties.loadFromXML(fis);
		} catch (InvalidPropertiesFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}

		// START CONFIG
		scheduleTickInterval = Integer.parseInt(properties
				.getProperty("scheduleTickInterval"));
		tdToolFilePath = properties.getProperty("tdToolFilePath");
		tellstickDeviceFilePath = properties
				.getProperty("tellstickDeviceFilePath");
		tellstickRepetitions = Integer.parseInt(properties
				.getProperty("tellstickRepetitions"));
		latitude = Double.parseDouble(properties
				.getProperty("latitude"));
		longitude = Double.parseDouble(properties
				.getProperty("longitude"));
		timeZone = properties.getProperty("timeZone");
		
		// END CONFIG
	}

	private static void DEBUG(String debugString) {
		Macros.DEBUG(debugString);
	}
}
